import {PageContainer} from "@ant-design/pro-components";
import InfraFileTable from "@/pages/InfraFile/components/InfraFileTable";
import {InfraFileVO, saveInfraFile, updateInfraFile} from "@/services/InfraFileService";
import InfraFileUpdateModal from "@/pages/InfraFile/components/InfraFileUpdateModal";
import {useRef, useState} from "react";
import {message} from "antd";
import {ActionType} from "@ant-design/pro-table";
import {useSnapshot} from "@@/exports";
import {InfraFileModel} from "@/models/infraFile";


const InfraFile = () => {
    // 表格引用
    const tableRef = useRef<ActionType>()
    let infraFileState = useSnapshot(InfraFileModel.state);

    // 提交修改
    const onUpdateFinish = async (formData: any) => {
        let dto: InfraFileVO = JSON.parse(JSON.stringify(formData))
        // 字段处理

        let func = infraFileState.infraFile ? updateInfraFile : saveInfraFile
        if (await func(dto)) {
            message.success("操作成功");
            // 刷新表格
            tableRef.current?.reload();
            return true;
        }
        return false;
    }

    return <PageContainer title="文件">
        {/*文件表格 开始*/}
        <InfraFileTable tableRef={tableRef} />
        {/*文件表格 结束*/}

        {/*修改模态框 开始*/}
        <InfraFileUpdateModal onFinish={onUpdateFinish}/>
        {/*修改模态框 结束*/}
    </PageContainer>
}

export default InfraFile
